package com.liry.security.config;

import com.liry.security.domain.entity.SysUser;
import java.util.Collection;
import java.util.stream.Collectors;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

/**
 * @author ALI
 * @since 2023/6/3
 */
@Data
public class CustomUser implements UserDetails {

    private static final long serialVersionUID = 5469888959861441262L;

    protected String userId;

    protected String password;

    protected String username;

    protected Collection<? extends GrantedAuthority> authorities;

    public CustomUser() {

    }

    public CustomUser(SysUser sysUser) {
        this.userId = sysUser.getId();
        this.username = sysUser.getUsername();
        this.password = sysUser.getPassword();
        if (!CollectionUtils.isEmpty(sysUser.getRoles())) {
            this.authorities = sysUser.getRoles().stream().map(d -> new SimpleGrantedAuthority("ROLE_" + d)).collect(Collectors.toList());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
